package com.zwz.crawler.mangeInfo;

import com.zwz.constant.Constant;
import com.zwz.entity.CourtAnnouncement;
import com.zwz.parse.JsonParserUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

import static com.zwz.constant.Constant.ENTERPRISE_ID;

@Slf4j
public class TungeeCourtCaseQuery {
//    private static final String ENTERPRISE_ID = "577de19eadf21dc4";
    private static final int START = 0;
    private static final int END = 5;

    public static List<CourtAnnouncement> startGetCourtInfo(String ENTERPRISE_ID ) {

        List<CourtAnnouncement> res=null;
        try {
            // 构建严格编码的URL
            String encodedId = URLEncoder.encode(ENTERPRISE_ID, String.valueOf(StandardCharsets.UTF_8));
            String url = String.format(
                    "https://sales.tungee.com/api/enterprise/detail/court-case-list?enterprise_id=%s&start=%d&end=%d",
                    encodedId, START, END
            );

            // 发送严格配置的请求
            Connection.Response response = Jsoup.connect(url)
                    .method(Connection.Method.GET)
                    .header("authority", "sales.tungee.com")
                    .header("accept", "*/*")
                    .header("accept-encoding", "gzip, deflate, br, zstd")
                    .header("accept-language", "zh-CN,zh;q=0.9")
                    .header("cache-control", "no-cache")
                    .header("cookie", Constant.cookies)
                    .header("pragma", "no-cache")
                    .header("priority", "u=1, i")
                    .header("referer", "https://sales.tungee.com/enterprise-details/"+ENTERPRISE_ID+"/enterprise-information/business-risk")
                    .header("sec-ch-ua", "\"Not;A=Brand\";v=\"24\", \"Chromium\";v=\"128\"")
                    .header("sec-ch-ua-mobile", "?0")
                    .header("sec-ch-ua-platform", "\"Windows\"")
                    .header("sec-fetch-dest", "empty")
                    .header("sec-fetch-mode", "cors")
                    .header("sec-fetch-site", "same-origin")
                    .header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36")
                    .header("x-tonxis-pid", "67864ea75f69e05d3b544ca7")
                    .ignoreContentType(true)
                    .ignoreHttpErrors(true)
                    .timeout(30000)
                    .execute();

            // 输出原始响应
           log.info("HTTP状态码: " + response.statusCode());
           log.info("响应内容:\n" + response.body());


            //解析内容
           res = JsonParserUtil.parseCourt(response.body());
        } catch (IOException e) {
            System.err.println("请求失败: " + e.getMessage());
        }

        return res;
    }


}